package leetcode.slidwidow;

public class Lc443 {

    public int compress(char[] chars) {
        int cnt = 0;
        int i = 0, j = 0;
        for (; j < chars.length; j++) {
            if (chars[i] != chars[j]) {
                if (cnt > 1) {
                    String temp = String.valueOf(cnt);
                    for (int k = 0; k < temp.length(); k++) {
                        chars[i + k + 1] = temp.charAt(k);
                    }
                    for (int k = i + temp.length() + 1; k < j; k++) {
                        chars[k] = ' ';
                    }
                }
                i = j;
                cnt = 1;
            } else cnt++;
        }
        if (cnt > 1) {
            String temp = String.valueOf(cnt);
            for (int k = 0; k < temp.length(); k++) {
                chars[i + k + 1] = temp.charAt(k);
            }
            for (int k = i + temp.length() + 1; k < j; k++) {
                chars[k] = ' ';
            }
        }
        i = 0;
        for (int k = 0; k < chars.length; k++) {
            if (chars[k] != ' ') chars[i++] = chars[k];
        }
        for (int k = 0; k < chars.length; k++) {
            System.out.print(chars[k]);
        }
        return i;
    }

    public static void main(String[] args) {
        Lc443 lc443 = new Lc443();
        System.out.println(lc443.compress(new char[]{'a','b','b','b','b','b','b','b','b','b','b','b','b','b','b','b','b'}));
    }
}
